package com.taotao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.*;
import com.taotao.service.ItemService;
import com.taotao.util.IDUtils;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by new on 2018/5/31.
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbItemDescMapper tbItemDescMapper;

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Autowired
    private SolrServer solrServer;

    @Override
    public TbItem getItemById(long id) {
        //通过条件查询
//        TbItemExample tbItemExample = new TbItemExample();
//        TbItemExample.Criteria criteria = tbItemExample.createCriteria();
//        criteria.andIdEqualTo(id);
//        List<TbItem> list = tbItemMapper.selectByExample(tbItemExample);
//        if(list != null && list.size() > 0){
//            return list.get(0);
//        }
        return tbItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbItem> selectList(TbItemExample record) {
        TbItemExample tbItemExample = new TbItemExample();
        List<TbItem> list = tbItemMapper.selectByExample(tbItemExample);
        return list;
    }

    @Override
    public EasyUIResult getItemList(int page, int rows) {

        TbItemExample tbItemExample = new TbItemExample();
        PageHelper.startPage(page,rows);
        List<TbItem> tbItems = tbItemMapper.selectByExample(tbItemExample);
        Page<TbItem> pageInfo = (Page<TbItem>)tbItems;
        EasyUIResult easyUIResult = new EasyUIResult();
        easyUIResult.setRows(tbItems);
        easyUIResult.setTotal((int) pageInfo.getTotal());
        return easyUIResult;
    }

    @Override
    public TaotaoResult createItem(TbItem item,String desc,String itemParam) throws Exception{
        Long id = IDUtils.genItemId();
        item.setId(id);
        item.setStatus((byte) 1);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        //添加商品基本信息
        tbItemMapper.insertSelective(item);
        //添加商品描述信息
        TaotaoResult taotaoResult = insertItemDesc(id, desc);
        //spring接受到异常就会回滚
        if(taotaoResult != null && taotaoResult.getStatus() != 200){
            throw new Exception();
        }
        //添加规格参数
        taotaoResult = insertItemParamItem(id,itemParam);
        if(taotaoResult != null && taotaoResult.getStatus() != 200){
            throw new Exception();
        }
        Item itemObject = tbItemMapper.getItemObjectById(id);

        try {
            //同步索引库
            //创建一个SolrInputDocument文档对象
            SolrInputDocument document = new SolrInputDocument();
            document.setField("id", itemObject.getId());
            document.setField("item_title", itemObject.getTitle());
            document.setField("item_sell_point", itemObject.getSell_point());
            document.setField("item_price", itemObject.getPrice());
            document.setField("item_image", itemObject.getImage());
            document.setField("item_category_name", itemObject.getCategory_name());
            document.setField("item_desc", itemObject.getItem_des());
            //写入索引库
            solrServer.add(document);
            //提交修改
            solrServer.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return TaotaoResult.ok();
    }

    private TaotaoResult insertItemDesc(Long id,String desc){

        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(id);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(new Date());
        itemDesc.setUpdated(new Date());
        tbItemDescMapper.insertSelective(itemDesc);
        return TaotaoResult.ok();
    }

    /**
     * 添加规格参数
     * @param itemId
     * @param itemParam
     * @return
     */
    private TaotaoResult insertItemParamItem(Long itemId,String itemParam){
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setItemId(itemId);
        tbItemParamItem.setParamData(itemParam);
        tbItemParamItem.setCreated(new Date());
        tbItemParamItem.setUpdated(new Date());
        tbItemParamItemMapper.insert(tbItemParamItem);
        return TaotaoResult.ok();
    }
}
